import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
import request from '../utils/request';
export const useProjectStore = defineStore('project', () => {
	// 状态
	const projects = ref([]);
	const currentPage = ref(1);
	const pageSize = ref(7);
	const total = ref(0);
	
	// 加载状态
	const isLoading = ref(false);
	const isError = ref(false);
	const isLoaded = ref(false);
	
	// 计算属性
	const hasMore = computed(() => {
		return projects.value.length < total.value;
	});
	
	const isEmpty = computed(() => {
		return isLoaded.value && projects.value.length === 0;
	});
	
	// 方法
	const fetchProjects = async (page = 1) => {
		try {
			isLoading.value = true;
			isError.value = false;
			
			const res = await request({
				url: '/app/project/project/page',
				method: 'POST',
				data: {
					page: page,
					size: pageSize.value
				}
			});

			const { data : { list, pagination } } = res;
			
			if (page === 1) {
				projects.value = list;
			} else {
				projects.value = [...projects.value, ...list];
			}
			
			currentPage.value = page;
			total.value = pagination?.total;
			isLoaded.value = true;
		} catch (error) {
			console.error('fetchProjects error:', error);
			isError.value = true;
			throw error;
		} finally {
			isLoading.value = false;
		}

		console.log('isLoaded = ' + isLoaded.value + ' isError = ' + isError.value + ' isLoading = ' + isLoading.value)
	};

	const addProject = async (projectData) => {
		try {
			const response = await request({
				url: '/app/project/project/add',
				method: 'POST',
				data: {
					...projectData,
				}
			});
		} catch (error) {
			console.error('addProject_ error:', error);
			throw error;
		}
	};

	const updateProject = async (projectData) => {
		try {
			const response = await request({
				url: '/app/project/project/update',
				method: 'POST',
				data: projectData
			});
			console.log('updateProject_ response:', response);
			
			// 本地更新数据
			const index = projects.value.findIndex(p => p.id === projectData.id);
			if (index !== -1) {
				projects.value[index] = {
					...projects.value[index],
					...projectData,
					updateTime: new Date().toISOString()
				};
			}
		} catch (error) {
			console.error('updateProject_ error:', error);
			throw error;
		}
	};

	const deleteProject = async (id) => {
		try {
			await request({
				url: '/app/project/project/delete',
				method: 'POST',
				data: {
					ids: [id]
				}
			});
		} catch (error) {
			console.error('deleteProject_ error:', error);
			throw error;
		}
	};

	const addProjectMember = async (projectId, userId) => {
		try {
			const response = await request({
				url: '/app/project/member/add',
				method: 'POST',
				data: { projectId, userId }
			});

			console.log('addProjectMember_ response:', response);
		} catch (error) {
			console.error('addProjectMember_ error:', error);
			throw new Error(error.message);
		}
	};

	return {
		// 状态
		projects,
		currentPage,
		pageSize,
		total,
		isLoading,
		isError,
		isLoaded,
		
		// 计算属性
		hasMore,
		isEmpty,
		
		// 方法
		fetchProjects,
		addProject,
		updateProject,
		deleteProject,
		addProjectMember
	};
});